package com.example.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Content;
import com.example.springboot.service.ContentService;

@CrossOrigin
@RestController

@RequestMapping("/content")
public class ContentController {
    @Autowired
    ContentService contentService;
    
    
    @PostMapping("/add")
    public Result add(@RequestBody Content content){
        try{
            contentService.save(content);
        }catch(Exception e){
            return Result.error("插入数据错误");
        }
        return Result.success();
    }
    
    @PutMapping("/update")
    public Result update(@RequestBody Content content){
        try{
            contentService.updateById(content);
        }catch(Exception e){
            return Result.error("更新数据错误");
        }
        return Result.success();
    }
    
    @DeleteMapping("/delete/{plate}")
    public Result delete(@PathVariable String plate){
        try{
            contentService.removeById(plate);
        }catch(Exception e){
            return Result.error("删除数据错误");
        }
        return Result.success();
    }
    
    
    
    @GetMapping("/selectByPlate/{plate}")
        public Result selectByPlate(@PathVariable String plate){
        Content content=contentService.getById(plate);
        return Result.success(content);
    }
}
